package com.yuanxiao.common.api.feign;

import com.yuanxiao.common.api.feign.fallback.FeignCallbackAPIFallbackClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.yuanxiao.common.core.result.R;

import java.net.URI;
import java.util.Map;

/**
 * Author: cg
 * Date: 2022-10-21 17:21
 * Content:
 */
// 注意：这里的url属性值不能为空字符串，但是可以设置为任意字符串值，在这里设置为“EMPTY”
@FeignClient(value = "feignCallbackAPI", url = "EMPTY", configuration = FeignCallbackConfiguration.class,fallback = FeignCallbackAPIFallbackClient.class)
@Qualifier("feignCallbackAPI")
public interface FeignCallbackAPI {
    /**
     * 统一回调接口方法，请求消息体格式为JSON，响应消息体格式也为JSON
     *
     * @param host     接口主机地址，如：http://localhost:8080
     * @param path     接口路径，如：/test/hello
     * @param queryMap 动态URL参数集合
     * @param body     请求消息体对象
     * @return
     */
    @RequestMapping(value = "{path}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    R postCallback(URI host,
               @PathVariable("path") String path,
               @SpringQueryMap Map<String, Object> queryMap,
               @RequestBody Object body);

    @RequestMapping(value = "{path}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    R putCallback(URI host,
               @PathVariable("path") String path,
               @SpringQueryMap Map<String, Object> queryMap,
               @RequestBody Object body);

    @RequestMapping(value = "{path}", method = RequestMethod.DELETE, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    R deleteCallback(URI host,
               @PathVariable("path") String path,
               @SpringQueryMap Map<String, Object> queryMap,
               @RequestBody Object body);

    @RequestMapping(value = "{path}", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    R getCallback(URI host,
               @PathVariable("path") String path,
               @SpringQueryMap Map<String, Object> queryMap);

    /**
     * 统一回调接口方法，请求消息体格式为JSON，响应消息体格式也为JSON
     *
     * @param uri      完整的请求路径地址，如：http://localhost:8080/test/hello
     * @param queryMap 动态URL参数集合
     * @param body     请求消息体对象
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    R callback(URI uri,
                    @SpringQueryMap Map<String, Object> queryMap,
                    @RequestBody Object body);
}

// 回调接口配置:预留
class FeignCallbackConfiguration {

//    /**
//     * 编码器：作用于请求阶段。将对象编码到Http请求体中。当参数没有标注@Param注解时候，编码器就会生效
//     */
//    @Bean
//    public Encoder feignEncoder() {
//        return new GsonEncoder();
//    }
//
//    /**
//     * 解码器，作用域响应阶段，解析HTTP 响应消息。
//     */
//    @Bean
//    public Decoder feignDecoder() {
//        return new GsonDecoder();
//    }
//
//    /**
//     * 重试机制
//     * @return
//     */
//    @Bean
//    public Retryer feignRetryer() {
//        return new Retryer.Default();
//    }
//
//    @Bean
//    public Logger feignLogger() {
//        return new Slf4jLogger();
//    }
//
//    @Bean
//    public Logger.Level feignLoggerLevel() {
//        return Logger.Level.FULL;
//    }


}